package com.llkj.qingnianguoxue.book;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.SearchSubcribeAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.BookVideoBean;
import com.llkj.qingnianguoxue.bean.BookVideoListBean;
import com.llkj.qingnianguoxue.bean.BuyVideoBean;
import com.llkj.qingnianguoxue.bean.SearchResultBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.home.BookDetailActivity;
import com.llkj.qingnianguoxue.home.MasterDetailActivity;
import com.llkj.qingnianguoxue.home.VideoDetailActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.llkj.qingnianguoxue.home.JingXuanActivity.REQUEST_VIDEO_CODE;

/**
 * 搜索订阅
 */
public class SearchSubscribeActivity extends BaseActivity  implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.et_search)
    protected EditText mEtSearch;
    @BindView(R.id.tv_search)
    protected TextView tvSearch;
    @BindView(R.id.refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    protected RecyclerView lvSearchList;//
    private SearchSubcribeAdapter mSubcribeAdapter;
    private List<SearchResultBean.ResultBean> resultData;
    private int totalSize, mPage, mCurrentPosition;
    private String mSearchKey;
    private String mSearchCategory;//1视频，2图书，3导师

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_subscribe);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        mSearchCategory = intent.getStringExtra("searchCategory");

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        RelativeLayout rlTop = findViewById(R.id.rl_top);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTop,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvSearchList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        resultData = new ArrayList<>();
        mSubcribeAdapter = new SearchSubcribeAdapter(R.layout.item_search_result,resultData);
        lvSearchList.setAdapter(mSubcribeAdapter);

        refreshLayout.setOnRefreshListener(this);
//        mSubcribeAdapter.setOnLoadMoreListener(this);
    }

    private void initListener(){
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchKey = mEtSearch.getText().toString();
                if (!StringUtils.isEmpty(mSearchKey)) {
                    loadWaitDialog();
                    onRefresh();
                } else {
                    ToastUtil.makeLongText(SearchSubscribeActivity.this, "请输入搜索关键字");
                }
            }
        });

        lvSearchList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                //需要根据类型，决定进入那个详情界面
                SearchResultBean.ResultBean item = resultData.get(i);
                if("1".equals(mSearchCategory)){
                    Bundle bundle = new Bundle();
                    bundle.putString("id", item.getId());
                    startActivity(VideoDetailActivity.class, bundle);
                }else if("2".equals(mSearchCategory)){
                    Bundle bundle = new Bundle();
                    bundle.putString("id", item.getId());
                    startActivity(BookDetailActivity.class, bundle);
                }else if("3".equals(mSearchCategory)){
                    Bundle bundle = new Bundle();
                    bundle.putString("id",item.getId());
                    startActivity(MasterDetailActivity.class, bundle);
                }
            }
        });
    }

    @OnClick({R.id.iv_back})
   public void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_USER_USERBUY_VIDEO) {
            ResponseList<BuyVideoBean> hotRankingBean = GsonUtil.fromJsonList(data, BuyVideoBean.class);
            if (hotRankingBean != null) {
                try{
                    totalSize = Integer.parseInt(hotRankingBean.getNum());
                }catch (Exception e){}

                if (mPage == 1) {
                    resultData.clear();
                    LoadDataHelper.refreshComplete(mSubcribeAdapter, resultData, refreshLayout, lvSearchList);
                } else {
                    mSubcribeAdapter.setEnableLoadMore(true);
                    mSubcribeAdapter.loadMoreComplete();
                }
                mSubcribeAdapter.loadMoreEnd();

                List<BuyVideoBean> list = hotRankingBean.getVideo();
                if(list != null){
                    BuyVideoBean buyVideoBean = null;
                    BookVideoListBean.CategoryBean categoryBean = null;
                    BookVideoBean item = null;
                    SearchResultBean.ResultBean resultBean = null;
                    for(int i=0;i<list.size();i++){
                        buyVideoBean = list.get(i);
                        for(int j=0;buyVideoBean!=null && j<buyVideoBean.getVideo().size();j++){
                            item = buyVideoBean.getVideo().get(j);
                            resultBean = new SearchResultBean.ResultBean();
                            resultBean.setName(item.getTitle());
                            resultBean.setId(item.getId());
                            resultData.add(resultBean);
                        }
                    }
                    mSubcribeAdapter.notifyDataSetChanged();
                }
            }
        }else if (method == HttpMethod.HTTP_USER_USERBUY_BOOKS) {
            ResponseList<BuyVideoBean> hotRankingBean = GsonUtil.fromJsonList(data, BuyVideoBean.class);
            if (hotRankingBean != null) {
                try {
                    totalSize = Integer.parseInt(hotRankingBean.getNum());
                } catch (Exception e) {
                }

                if (mPage == 1) {
                    resultData.clear();
                    LoadDataHelper.refreshComplete(mSubcribeAdapter, resultData, refreshLayout, lvSearchList);
                } else {
                    mSubcribeAdapter.setEnableLoadMore(true);
                    mSubcribeAdapter.loadMoreComplete();
                }
                mSubcribeAdapter.loadMoreEnd();

                List<BuyVideoBean> list = hotRankingBean.getVideo();
                if (list != null) {
                    BuyVideoBean buyVideoBean = null;
                    BookVideoListBean.CategoryBean categoryBean = null;
                    BookVideoBean item = null;
                    SearchResultBean.ResultBean resultBean = null;
                    for (int i = 0; i < list.size(); i++) {
                        buyVideoBean = list.get(i);
                        for (int j = 0; buyVideoBean != null && j < buyVideoBean.getBooks().size(); j++) {
                            item = buyVideoBean.getBooks().get(j);
                            resultBean = new SearchResultBean.ResultBean();
                            resultBean.setName(item.getTitle());
                            resultBean.setId(item.getId());
                            resultData.add(resultBean);
                        }
                    }
                    mSubcribeAdapter.notifyDataSetChanged();
                }
            }
        }else if (method == HttpMethod.HTTP_USER_USERBUY_ARTIST) {
            BookVideoListBean hotRankingBean = GsonUtil.GsonToBean(data, BookVideoListBean.class);
            if (hotRankingBean != null) {
                try {
                    totalSize = Integer.parseInt(hotRankingBean.getNum());
                } catch (Exception e) {
                }

                if (mPage == 1) {
                    resultData.clear();
                    LoadDataHelper.refreshComplete(mSubcribeAdapter, resultData, refreshLayout, lvSearchList);
                } else {
                    mSubcribeAdapter.setEnableLoadMore(true);
                    mSubcribeAdapter.loadMoreComplete();
                }
                mSubcribeAdapter.loadMoreEnd();

                BookVideoBean item = null;
                SearchResultBean.ResultBean resultBean = null;
                for (int j = 0; hotRankingBean.getArtist() != null && j < hotRankingBean.getArtist().size(); j++) {
                    item = hotRankingBean.getArtist().get(j);
                    resultBean = new SearchResultBean.ResultBean();
                    resultBean.setName(item.getName());
                    resultBean.setId(item.getId());
                    resultData.add(resultBean);
                }
                mSubcribeAdapter.notifyDataSetChanged();
            }
        }
    }

    private void getVideoData() {
        HttpMethod.userbuyVideo(this, this, "2", String.valueOf(mPage),mSearchKey);
    }

    private void getBookData(){
        HttpMethod.userbuyBooks(this, this, "2", String.valueOf(mPage),mSearchKey);
    }

    private void getMasterData() {
        HttpMethod.userbuyArtist(this, this, mSearchKey, String.valueOf(mPage));
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        if("1".equals(mSearchCategory)){
            getVideoData();
        }else if("2".equals(mSearchCategory)){
            getBookData();
        }else if("3".equals(mSearchCategory)){
            getMasterData();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        mPage++;
        if("1".equals(mSearchCategory)){
            getVideoData();
        }else if("2".equals(mSearchCategory)){
            getBookData();
        }else if("3".equals(mSearchCategory)){
            getMasterData();
        }
    }

    @Override
    public void onLoadMore() {
        LoadDataHelper.loadMoreRequest(this, mSubcribeAdapter, null, lvSearchList, totalSize);
    }
}
